package com.xiaoge.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.security.UserInfo;
import com.xiaoge.biz.entity.InStock;
import com.xiaoge.biz.entity.InStockInfo;
import com.xiaoge.biz.entity.ProductStock;
import com.xiaoge.biz.mapper.InStockInfoMapper;
import com.xiaoge.biz.mapper.InStockMapper;
import com.xiaoge.biz.mapper.ProductStockMapper;
import com.xiaoge.biz.service.InStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoge.biz.vo.IntoStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@Service
public class InStockServiceImpl extends ServiceImpl<InStockMapper, InStock> implements InStockService {
    @Autowired
    private InStockInfoMapper inStockInfoMapper;
    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private ProductStockMapper productStockMapper;

    /**
     * 分页查询入库列表
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<InStock> findInStockList(Page<InStock> page, QueryWrapper<InStock> queryWrapper) {
        return baseMapper.findInStockList(page, queryWrapper);
    }

    @Override
    public void addIntoStock(IntoStockVo intoStockVo) {
        //随机生成入库单号
        String IN_STOCK_NUM = UUID.randomUUID().toString().substring(0, 32);
        int itemNumber = 0;//记录该单的总数
        List<InStockInfo> products = intoStockVo.getProducts();
        //添加入库明细
        if (!ObjectUtils.isEmpty(products)) {
            for (InStockInfo product : products) {
                itemNumber += product.getProductNumber();
                product.setInNum(IN_STOCK_NUM);
                inStockInfoMapper.insert(product);
            }
        }

        InStock inStock = new InStock();
        inStock.setInNum(IN_STOCK_NUM);
        inStock.setProductNumber(itemNumber);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            UserInfo userInfo = (UserInfo) authentication.getPrincipal();
            inStock.setOperator(userInfo.getUsername());
        }
        if (intoStockVo.getSupplierId() != null) {
            inStock.setSupplierId(intoStockVo.getSupplierId());
        }
        inStock.setStatus(2);
        inStock.setRemark(intoStockVo.getRemark());
        inStock.setType(intoStockVo.getType());
        inStockMapper.insert(inStock);
    }

    /**
     * 审核通过
     *
     * @param id
     * @param inNum
     */
    @Override
    public void publishInStock(Long id, String inNum) {
        InStock inStock = new InStock();
        QueryWrapper<InStockInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("in_num", inNum);
        List<InStockInfo> inStockInfos = inStockInfoMapper.selectList(queryWrapper);
        if (!ObjectUtils.isEmpty(inStockInfos)) {
            for (InStockInfo inStockInfo : inStockInfos) {
                String pNum = inStockInfo.getPNum();//物资编号
                Integer productNumber = inStockInfo.getProductNumber();//入库物资数
                QueryWrapper<ProductStock> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("p_num", pNum);
                ProductStock productStock = productStockMapper.selectOne(queryWrapper1);
                //仓库中已有这个物品
                if (!ObjectUtils.isEmpty(productStock)) {
                    productStock.setStock(productStock.getStock() + (long) productNumber);
                    productStockMapper.updateById(productStock);
                } else {
                    ProductStock productStock1 = new ProductStock();
                    productStock1.setPNum(pNum);
                    productStock1.setStock((long) productNumber);
                    productStockMapper.insert(productStock1);
                }
            }
            inStock.setId(id);
            inStock.setStatus(0);
            inStock.setCreateTime(new Date());
            inStockMapper.updateById(inStock);
        }
    }

    /**
     * 删除入库清单
     *
     * @param id
     * @param inNum
     */
    @Override
    public void delete(Long id, String inNum) {
        QueryWrapper<InStockInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("in_num", inNum);
        inStockInfoMapper.delete(queryWrapper);
        inStockMapper.deleteById(id);
    }
}
